package ro.msg.quiz.controller;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import ro.msg.quiz.domain.MultipleChoiceQuestion;
import ro.msg.quiz.domain.Question;
import ro.msg.quiz.domain.Quiz;
import ro.msg.quiz.domain.SingleChoiceQuestion;
import ro.msg.quiz.domain.TextQuestion;
import ro.msg.quiz.repository.Repository;

//AUTHORS: Roxana

//Class responsible for creating a xml file from the data in repository
public class ExportXML {
    private Repository repo = Repository.getInstance();

    public ExportXML() {
	export();
    }

    // converts an ArrayList<Question> to xml

    public void convertQuestionList(Element e, Document doc, List<Question> ql) {
	for (int i = 0; i < ql.size(); i++) {
	    Element question = doc.createElement("question");
	    e.appendChild(question);

	    Element id = doc.createElement("id");
	    id.appendChild(doc.createTextNode(Integer.toString(ql.get(i).getId())));
	    question.appendChild(id);

	    Element text = doc.createElement("text");
	    text.appendChild(doc.createTextNode(ql.get(i).getText()));
	    question.appendChild(text);

	    Element type = doc.createElement("type");
	    type.appendChild(doc.createTextNode(ql.get(i).getType().toString()));
	    question.appendChild(type);

	    if (ql.get(i).getType().toString().equals("SINGLE_CHOICE_Q")) {
		Element options = doc.createElement("options");
		question.appendChild(options);

		for (int j = 0; j < ((SingleChoiceQuestion) ql.get(i)).getOptions().size(); j++) {
		    Element option = doc.createElement("option");
		    option.appendChild(doc.createTextNode(((SingleChoiceQuestion) ql.get(i)).getOptions().get(j)));
		    options.appendChild(option);
		}

		Element correctIndex = doc.createElement("correctIndex");
		correctIndex.appendChild(doc.createTextNode(Integer.toString(((SingleChoiceQuestion) ql.get(i)).getCorrectIndex())));
		question.appendChild(correctIndex);

	    } else if (ql.get(i).getType().toString().equals("MULTIPLE_CHOICE_Q")) {
		Element options = doc.createElement("options");
		question.appendChild(options);

		for (int j = 0; j < ((MultipleChoiceQuestion) ql.get(i)).getOptions().size(); j++) {
		    Element option = doc.createElement("option");
		    option.appendChild(doc.createTextNode(((MultipleChoiceQuestion) ql.get(i)).getOptions().get(j)));
		    options.appendChild(option);
		}

		Element correctIndexes = doc.createElement("correctIndexes");
		question.appendChild(correctIndexes);
		for (int j = 0; j < ((MultipleChoiceQuestion) ql.get(i)).getCorrectIndexes().size(); j++) {
		    Element index = doc.createElement("index");
		    index.appendChild(doc.createTextNode(((MultipleChoiceQuestion) ql.get(i)).getCorrectIndexes().get(j).toString()));
		    correctIndexes.appendChild(index);
		}

	    } else {
		Element answer = doc.createElement("answer");
		answer.appendChild(doc.createTextNode(((TextQuestion) ql.get(i)).getAnswer()));
		question.appendChild(answer);
	    }

	}
    }

    // exports list of quizzes and list of questions to xml
    public void export() {
	List<Question> questionlist = new ArrayList<Question>();
	List<Quiz> quizzeslist = new ArrayList<Quiz>();
	quizzeslist = repo.getQuizzesList();
	try {
	    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
	    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

	    Document doc = docBuilder.newDocument();
	    Element rootElement = doc.createElement("SmartQuestionnaire");
	    doc.appendChild(rootElement);

	    Element questions = doc.createElement("Questions");
	    rootElement.appendChild(questions);
	    Element quizzes = doc.createElement("Quizzes");
	    rootElement.appendChild(quizzes);

	    for (int j = 0; j < quizzeslist.size(); j++) {
		Element quizz = doc.createElement("quizz");
		quizzes.appendChild(quizz);

		Element quizzId = doc.createElement("quizzId");
		quizzId.appendChild(doc.createTextNode(Integer.toString(quizzeslist.get(j).getId())));
		quizz.appendChild(quizzId);

		Element name = doc.createElement("name");
		name.appendChild(doc.createTextNode(quizzeslist.get(j).getName()));
		quizz.appendChild(name);

		Element quizzQuestions = doc.createElement("questions");
		questionlist = quizzeslist.get(j).getQuestionList();
		convertQuestionList(quizzQuestions, doc, questionlist);
		quizz.appendChild(quizzQuestions);
	    }
	    questionlist = repo.getQuestionsList();
	    convertQuestionList(questions, doc, questionlist);

	    TransformerFactory transformerFactory = TransformerFactory.newInstance();
	    Transformer transformer = transformerFactory.newTransformer();
	    DOMSource source = new DOMSource(doc);
	    StreamResult result = new StreamResult(new File("questionnaire.xml"));
	    transformer.transform(source, result);

	} catch (ParserConfigurationException pce) {
	    pce.printStackTrace();
	} catch (TransformerException tfe) {
	    tfe.printStackTrace();
	}
    }
}
